
package com.hlkj.pay.controller.merchant;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.core.bean.BeanUtil;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.merchant.IMerchantInfoApplicationAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.common.RsaConfig;
import com.hlkj.pay.dto.merchant.CashierConfigDto;
import com.hlkj.pay.dto.merchant.MerchantAppQueryDto;
import com.hlkj.pay.dto.merchant.RouterConfigDto;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppDO;
import com.hlkj.pay.service.common.IPlatFormConfigService;
import com.hlkj.pay.service.merchant.IMerchantApplicationService;
import com.hlkj.pay.vo.common.req.CommonUpdateStatusReq;
import com.hlkj.pay.vo.merchant.req.app.MerchantAppPageReq;
import com.hlkj.pay.vo.merchant.resp.app.MerchantAppResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author HlpayTeam
 * @date 2024/09/12 11:32
 */
@Tag(name = "商户应用管理")
@RestController
@RequestMapping("/v1/merchant/app")
@RequiredArgsConstructor
public class MerchantAppController extends BaseController {

    private final IMerchantApplicationService merchantApplicationService;
    private final IMerchantInfoApplicationAppService merchantInfoApplicationAppService;
    private final IPlatFormConfigService platFormConfigService;
    

    @Operation(summary = "商户应用列表分页")
    @GetMapping("/page")
    CommonResult<PageResult<MerchantAppResp>> queryPage(@Validated MerchantAppPageReq merchantAppPageReq) {
        MerchantAppQueryDto merchantInfoQueryDO = new MerchantAppQueryDto();
        merchantInfoQueryDO.setLikeAppName(merchantAppPageReq.getAppName());
        merchantInfoQueryDO.setLikeAppId(merchantAppPageReq.getAppId());
        merchantInfoQueryDO.setSysType(merchantAppPageReq.getSysType());
        merchantInfoQueryDO.setLikeSn(merchantAppPageReq.getSn());
        merchantInfoQueryDO.setPageNo(merchantAppPageReq.getPageNo());
        merchantInfoQueryDO.setPageSize(merchantAppPageReq.getPageSize());
        PageResult<MerchantAppDO> page = merchantApplicationService.queryPage(merchantInfoQueryDO);
        List<MerchantAppDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<MerchantAppResp> merchantInfoPageResps = BeanUtil.copyToList(list, MerchantAppResp.class);
        processMerchantList(merchantInfoPageResps);
        return buildPageResult(page, merchantInfoPageResps);
    }
    @Operation(summary = "商户应用列表不分页")
    @GetMapping("")
    CommonResult<List<MerchantAppResp>> queryList(@Validated MerchantAppPageReq merchantAppPageReq) {
        MerchantAppQueryDto merchantInfoQueryDO = new MerchantAppQueryDto();
        merchantInfoQueryDO.setLikeAppName(merchantAppPageReq.getAppName());
        merchantInfoQueryDO.setLikeAppId(merchantAppPageReq.getAppId());
        merchantInfoQueryDO.setSysType(merchantAppPageReq.getSysType());
        merchantInfoQueryDO.setLikeSn(merchantAppPageReq.getSn());
        merchantInfoQueryDO.setPageNo(merchantAppPageReq.getPageNo());
        merchantInfoQueryDO.setPageSize(merchantAppPageReq.getPageSize());
        List<MerchantAppDO> list = merchantApplicationService.queryList(merchantInfoQueryDO);
        if (CollectionUtils.isEmpty(list)) {
            return CommonResult.success(Collections.emptyList());
        }
        List<MerchantAppResp> merchantInfoPageResps = BeanUtil.copyToList(list, MerchantAppResp.class);
        processMerchantList(merchantInfoPageResps);
        return CommonResult.success(merchantInfoPageResps);
    }
    @Operation(summary = "商户应用详情")
    @GetMapping("/detail")
    CommonResult<MerchantAppResp> detail(@RequestParam("id") Long id) {
        MerchantAppDO merchantAppDO = merchantApplicationService.queryDetail(id);
        if (merchantAppDO == null) {
            return CommonResult.success();
        }
        RsaConfig rsaConfig = platFormConfigService.getRsaConfig();
        MerchantAppResp merchantAppResp = BeanUtil.copyProperties(merchantAppDO, MerchantAppResp.class);
        merchantAppResp.setPlatPublicKey(rsaConfig.getPublicKey());
        merchantAppResp.setRouterConfig(merchantAppDO.getRouterConfigDto());
        merchantAppResp.setCashierConfig(merchantAppDO.getCashierConfigDto());
        return CommonResult.success(merchantAppResp);
    }

    @Operation(summary = "新增商户应用")
    @PostMapping("")
    CommonResult<String> saveMerchantInfo(@Validated @RequestBody MerchantAppResp merchantInfoUpdateReq) {
        return merchantInfoApplicationAppService.saveMerchantApp(merchantInfoUpdateReq);
    }
    @Operation(summary = "更新商户应用信息")
    @PutMapping("")
    CommonResult<Void> updateMerchantInfo(@Validated @RequestBody MerchantAppResp merchantInfoUpdateReq) {
        return merchantInfoApplicationAppService.updateMerchantApp(merchantInfoUpdateReq);
    }

    @Operation(summary = "更新商户应用状态")
    @PostMapping("/status")
    CommonResult<Void> updateStatus(@Validated @RequestBody CommonUpdateStatusReq commonUpdateStatusReq) {
        return merchantInfoApplicationAppService.updateMerchantAppStatus(commonUpdateStatusReq);
    }
    @Operation(summary = "删除商户应用")
    @DeleteMapping("")
    CommonResult<Void> delMerchantApp(@Validated @RequestBody CommonUpdateStatusReq commonUpdateStatusReq) {
        return merchantInfoApplicationAppService.delMerchantApp(commonUpdateStatusReq.getId());
    }

    @Operation(summary = "应用收银台链接")
    @PostMapping("/cashier/payment/url")
    CommonResult<String> cashierUrl(@Validated @RequestBody CommonUpdateStatusReq commonUpdateStatusReq) {
        return merchantApplicationService.cashierUrl(commonUpdateStatusReq.getId());
    }
    @Operation(summary = "轮询配置")
    @PostMapping("/router")
    CommonResult<Void> routerConfig(@Validated @RequestBody RouterConfigDto routerConfigDto) {
        return merchantInfoApplicationAppService.routerConfig(routerConfigDto);
    }
    @Operation(summary = "收银台配置")
    @PostMapping("/cashier")
    CommonResult<Void> cashierConfig(@Validated @RequestBody CashierConfigDto cashierConfigDto) {
        return merchantInfoApplicationAppService.cashierConfig(cashierConfigDto);
    }

}
